#!/bin/bash 
#add the file to measure how log the time provsion cost

date +%s > /tmp/provision-start.txt

YUM_SYS=`cat /etc/redhat-release |awk -F" " '{print$(NF-1)}'`
YUM_BASEARCH=`uname -i`

bold=$(tput bold)
normal=$(tput sgr0)

echo "Make sure you do provision under ${bold}DEVELOP${normal} branch!!"

echo "--> Checking if the files are Unix style"

[[ $(file add_new_disk.sh) =~ CRLF ]] && echo "Windows style. Make sure git's autocrlf is set to false before proceed!!" && exit 1

echo "--> Starting Vagrant Pre-provisioning: pre_provision.sh..." 

SOURCE_ROOT=..

# First thing first, since we are using YUM caching inside Vagrant (Very Important), let's 
# disable YUM CLEAN as soon as possible.
if [ ! -e /usr/share/yum-cli/cli.py.bak ]; then
     myline="/if 'all' in userlist:/" 
     mytext="\
\ \ \ \ \ \ \ \ # Added by stevel, Nov 29, 2014\n\
\ \ \ \ \ \ \ \ return 1, [_('YUM CLEAN disabled inside Vagrant at Sumilux, see #7712 ')]"
     sudo sed -i.bak -e "${myline} i${mytext}" /usr/share/yum-cli/cli.py
fi;

#Add dns
#dns=$(cat /etc/resolv.conf | grep "nameserver 8.8.8.8")
#if [ -z "$dns" ]; then
#	sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf
#	sudo echo "nameserver 8.8.4.4" >> /etc/resolv.conf
#	sudo echo "nameserver 114.114.114.114" >> /etc/resolv.conf 
#fi

# Add epel repo 
echo "--> Installing EPEL repo" 
if [ ! -e /etc/yum.repos.d/epel.repo ] && [[ "$YUM_SYS" =~ ^6\..* ]]; then
    echo "Installing EPEL repo..."
    # Note, $YUM_BASEARCH defined in function.sh
    sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/${YUM_BASEARCH}/epel-release-6-8.noarch.rpm
    # Never CLEAN YUM any more, see below
    # NEED_YUM_CLEAN=1
    echo "EPEL repo installed"
elif [ ! -e /etc/yum.repos.d/epel.repo ] && [[ "$YUM_SYS" =~ ^7\..* ]]; then
    echo "Installing EPEL repo..."
    # Note, $YUM_BASEARCH defined in function.sh ,the epel-release-7-6.noarch.rpm is not found now  update it to 7.8
    sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/${YUM_BASEARCH}/e/epel-release-7-8.noarch.rpm
    # Never CLEAN YUM any more, see below
    # NEED_YUM_CLEAN=1
    echo "EPEL repo installed"
fi

# Clean up previous installations
if  [ -e /etc/yum.repos.d/CentOS-6-Vagrant**.repo ]; then
   rm -f /etc/yum.repos.d/CentOS-6-Vagrant**.repo
fi

# https://www.certdepot.net/rhel7-get-started-delta-rpms/
echo "--> Install deltarpm"
if rpm -aq|grep -q deltarpm; then
   echo "deltarpm ready"
else
   yum install -y deltarpm
fi

echo "--> Install Puppet"
if rpm -aq|grep -q puppet; then
   echo "Puppet ready"
else
   yum install -y puppet
fi

# when the file is delete , the fz88,fz etc's provision can not get the right downlouad server host 
#if [ -e /tmp/download_server.txt ]; then
#    rm -f /tmp/download_server.txt
#fi
echo "--> Pre_provision finished in ${SECONDS}s"
